#!/usr/bin/python3
"""
Configure authentication sources using authconfig.

Applies the default settings. Backups are cleared.

Notes:
  - Requires 'chroot' in the buildroot.
  - Runs the 'authconfig' binary from the image in the chroot.
"""


import shutil
import subprocess
import sys

import osbuild.api


SCHEMA = """
"additionalProperties": false,
"description": "Configure authentication sources."
"""


def main(tree):
    cmd = [
        "/usr/sbin/chroot", tree,
        "/usr/sbin/authconfig", "--nostart",  "--updateall"
    ]

    subprocess.run(cmd, check=True)

    shutil.rmtree(f"{tree}/var/lib/authselect/backups", ignore_errors=True)

    return 0


if __name__ == '__main__':
    args = osbuild.api.arguments()
    r = main(args["tree"])
    sys.exit(r)
